//
//  HCRemarkListViewController.m
//  HelloCall
//
//  Created by WangYongpeng on 13-9-21.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCRemarkListViewController.h"
#import "HCRemarkListModel.h"
#import "HCNetworkManger.h"

@interface HCRemarkListViewController()<HCModelDataObserver>

@property (nonatomic, retain)UIActivityIndicatorView* activityIndicatorView;

@end

@implementation HCRemarkListViewController

@synthesize callInfo = m_callInfo;
@synthesize customInfo = m_customInfo;
@synthesize delegate = m_delegate;
@synthesize activityIndicatorView = m_activityIndicatorView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIBarButtonItem* menuItem = [[[UIBarButtonItem alloc] initWithTitle:@"添加备注" style:UIBarButtonItemStylePlain target:self action:@selector(addRemarkPress:)] autorelease];
    self.navigationItem.rightBarButtonItem = menuItem;
    
    self.title = @"话单备注";
    
    [[HCRemarkListModel defaultModel] addObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[HCRemarkListModel defaultModel] removeObserver:self];
    self.callInfo = nil;
    self.customInfo = nil;
    self.activityIndicatorView = nil;
    
    [super dealloc];
}

- (void)addRemarkPress:(UIBarButtonItem*)button
{
    
}

- (void)setCallInfo:(HCCallInfo *)callInfo
{
    [callInfo retain];
    [m_callInfo release];
    m_callInfo = callInfo;
    
    self.title = @"话单备注";
    
    [[HCNetworkManger shareManger] queryCallMemoByCustomId:callInfo.ticketID offset:0];
    m_activityIndicatorView = [[UIActivityIndicatorView alloc]
                               initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

- (void)setCustomInfo:(HCCRMCustomInfo *)customInfo
{
    [customInfo retain];
    [m_customInfo release];
    m_customInfo = customInfo;
    
    self.title = @"客户备注";
    
    [[HCNetworkManger shareManger] queryCallMemoByCustomId:customInfo.userID offset:0];
    m_activityIndicatorView = [[UIActivityIndicatorView alloc]
                               initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
}

- (NSArray*)getRemarkList
{
    if (nil != self.customInfo)
    {
        return [[HCRemarkListModel defaultModel] getRemarkListByCustId:self.customInfo.userID];
    }
    else if (nil != self.callInfo)
    {
        return [[HCRemarkListModel defaultModel] getRemarkListByTicketId:self.callInfo.ticketID];
    }

    return nil;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self getRemarkList].count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* key = @"RemarkInfoCell";
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:key];
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:key] autorelease];
    }
    
    HCCallRemarkInfo* remarkInfo = [[self getRemarkList] objectAtIndex:indexPath.row];
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd号 HH:mm:ss"];
    
    cell.textLabel.text = [dateFormatter stringFromDate:remarkInfo.addMemoDate];
    cell.textLabel.textColor = [UIColor blackColor];
    
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(showRemarkInfoDetail:)])
    {
        [self.delegate showRemarkInfoDetail:[[self getRemarkList] objectAtIndex:indexPath.row]];
    }
}

#pragma mark -- HCModelDataObserver
- (void)dataUpdateByMsgID:(int)msgID
{
    /*pause modify———————————————------
    switch (msgID)
    {
        //获取备注
        case EQueryCallMemo:
        {
            [self.tableView reloadData];
            
            [self.activityIndicatorView stopAnimating];
            self.activityIndicatorView = nil;
            
            break;
        }
        //添加备注
        case ERecordCallMemo:
        {
            break;
        }
        //修改备注
        case EUpdateCallMemo:
        {
            break;
        }
        default:
            break;
    }
    pause modify———————————————————*/
}

@end
